package com.test.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.test.ThreadApplication;
import com.test.service.MainService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;

@RestController
public class MainController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ThreadApplication.class);

    public static final String SUCCESS = "success";

    public static final String POOL_INFO = "poolSize:%d, maximumPoolSize:%d,activeThreadCount:%d";

    @Autowired
    private MainService mainService;

    /**
     * 查询用户待办信息
     * @param userId
     * @return
     * @throws InterruptedException
     */
    @GetMapping("/test1")
    public ResponseEntity<String> test1(final String userId) throws InterruptedException {

        final String uid = userId + " " + LocalDateTime.now();

        return ResponseEntity.ok(mainService.test1(uid));
    }

    /**
     * 查询用户待办信息
     * @param userId
     * @return
     * @throws InterruptedException
     */
    @GetMapping("/test2")
    public ResponseEntity<String> test2(final String userId) throws InterruptedException, ExecutionException {

        final String uid = userId + " " + LocalDateTime.now();

        return ResponseEntity.ok(mainService.test2(uid));
    }

    /**
     * 查询用户待办信息
     * @param userId
     * @return
     * @throws InterruptedException
     */
    @GetMapping("/test3")
    public ResponseEntity<String> test3(final String userId) throws InterruptedException, ExecutionException {

        final String uid = userId + " " + LocalDateTime.now();

        return ResponseEntity.ok(mainService.test3(uid));
    }

    /**
     * 查询用户待办信息
     * @param userId
     * @return
     * @throws InterruptedException
     */
    @GetMapping("/test4")
    public ResponseEntity<String> test4(final String userId) throws InterruptedException, ExecutionException {

        final String uid = userId + " " + LocalDateTime.now();

        return ResponseEntity.ok(mainService.test4(uid));
    }

    /**
     * 查询用户待办信息
     * @param userId
     * @return
     * @throws InterruptedException
     */
    @GetMapping("/test5")
    public ResponseEntity<String> test5(final String userId) throws InterruptedException, ExecutionException {

        final String uid = userId + " " + LocalDateTime.now();

        return ResponseEntity.ok(mainService.test5(uid));
    }

    /**
     * http://localhost:8080/showPool
     * @return
     * @throws JsonProcessingException
     */
    @GetMapping("/showPool")
    public ResponseEntity<String> showPool() throws JsonProcessingException {

        final ForkJoinPool forkJoinPool = ForkJoinPool.commonPool();
        final int poolSize = forkJoinPool.getPoolSize();
        final int maximumPoolSize = forkJoinPool.getParallelism();
        final int activeThreadCount = forkJoinPool.getActiveThreadCount();
        LOGGER.info("poolSize:{}, maximumPoolSize:{},activeThreadCount:{}", poolSize, maximumPoolSize, activeThreadCount);
        return ResponseEntity.ok(String.format(POOL_INFO, poolSize, maximumPoolSize, activeThreadCount));
    }


    /**
     * http://localhost:8080/mockBusy
     * @return
     * @throws JsonProcessingException
     */
    @GetMapping("/mockBusy")
    public ResponseEntity<String> mockBusy() throws ExecutionException, InterruptedException {

        final CompletableFuture<String> rs = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(100L);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "Hello";
        });

        return ResponseEntity.ok(rs.get());
    }
}
